/* ===========================================================
 * NetworkEvolutionPlugin : Cytoscape plugin for visualizing stages of
 * protein networks evolution.
 * ===========================================================
 *
 *
 * Project Info:  http://bioputer.mimuw.edu.pl/modevo/
 * Sources: http://code.google.com/p/misiek/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * NetworkEvolutionPlugin  Copyright (C) 2008-2010
 * Authors:  Michal Wozniak (code) (m.wozniak@mimuw.edu.pl)
 *           Janusz Dutkowski (idea, data) (j.dutkowski@mimuw.edu.pl)
 *           Jerzy Tiuryn (supervisor) (tiuryn@mimuw.edu.pl)
 */

package ppine.ui.dataloading;

import java.awt.event.ActionEvent;
import java.io.File;
import javax.help.HelpBroker;
import javax.swing.JFileChooser;
import ppine.help.PPINEHelpBroker;
import ppine.io.listeners.ExperimentsLoadingErrorsListener;
import ppine.main.LoadedDataHandle;
import ppine.main.PluginDataHandle;
import ppine.ui.UIController;
import ppine.ui.listeners.ExperimentsLoadedListener;

public class ExperimentsLoaderPanel extends javax.swing.JPanel {

    private String filepath;
    private DataLoaderPanel loaderPanel;
    private ExperimentsLoadedListener list;

    /** Creates new form ExperimentsLoadingPanel
     * @param loaderPanel
     * @param list
     */
    public ExperimentsLoaderPanel(DataLoaderPanel loaderPanel, ExperimentsLoadedListener list) {
        this.loaderPanel = loaderPanel;
        this.list = list;
        initComponents();
        initState();
    }

    public void initState() {
        LoadedDataHandle ldh = PluginDataHandle.getLoadedDataHandle();
        if (ldh.isExpLoaded()) {
            setLoadedState();
        } else {
            setUnloadedState();
        }
    //refreshStats();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chooseFile = new javax.swing.JButton();
        loadTreeButton = new javax.swing.JButton();
        cleanButton = new javax.swing.JButton();
        helpButton = new javax.swing.JButton();
        filenameLabel = new javax.swing.JLabel();

        chooseFile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ppine/resources/icons/com.png"))); // NOI18N
        chooseFile.setText("Choose file");
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ppine/ui/dataloading/Bundle"); // NOI18N
        chooseFile.setToolTipText(bundle.getString("ChooseFileFamiliesTreeButton.ToolTip_1")); // NOI18N
        chooseFile.setName("chooseFile"); // NOI18N
        chooseFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseFileActionPerformed(evt);
            }
        });

        loadTreeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ppine/resources/icons/save.png"))); // NOI18N
        loadTreeButton.setText("Load");
        loadTreeButton.setToolTipText(bundle.getString("LoadFamiliesTreesButton.ToolTip")); // NOI18N
        loadTreeButton.setName("loadTreeButton"); // NOI18N
        loadTreeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadTreeButtonActionPerformed(evt);
            }
        });

        cleanButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ppine/resources/icons/clean.png"))); // NOI18N
        cleanButton.setText("Clean");
        cleanButton.setToolTipText(bundle.getString("CleanSpeciesTree.ToolTip")); // NOI18N
        cleanButton.setName("cleanButton"); // NOI18N
        cleanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cleanButtonActionPerformed(evt);
            }
        });

        helpButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ppine/resources/icons/help.png"))); // NOI18N
        helpButton.setText("File format info");
        helpButton.setToolTipText(bundle.getString("FileFormatInfoSpecies.ToolTip")); // NOI18N
        helpButton.setName("helpButton"); // NOI18N
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });

        filenameLabel.setText("filename");
        filenameLabel.setName("filenameLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 460, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(chooseFile, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(loadTreeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cleanButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(helpButton, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(filenameLabel))
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(chooseFile, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(loadTreeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(helpButton)
                        .addComponent(cleanButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(filenameLabel)
                    .addContainerGap(231, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void chooseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseFileActionPerformed
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(fc);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            filepath = file.getAbsolutePath();
            filenameLabel.setText(filepath);
            PluginDataHandle.getLoadingDataHandle().setExpFilename(filepath);
            loadTreeButton.setEnabled(true);
        }
}//GEN-LAST:event_chooseFileActionPerformed

    private void loadTreeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadTreeButtonActionPerformed
        if (filepath != null) {

            ExperimentsLoadingErrorsListener errorListener = new ExperimentsLoadingErrorsListener(loaderPanel);
            DefaultLoadingController.loadExperimentsData(filepath, errorListener);
            setLoadedState();
            LoadedDataHandle ldh = PluginDataHandle.getLoadedDataHandle();
            ldh.setExperimentsFilename(filepath);

            list.actionPerformed(new ActionEvent(this, 2, "Experiments loaded"));
        //refreshStats();
//            SpeciesLoadingErrorsListener errorListener = new SpeciesLoadingErrorsListener(loaderPanel);
        //           DefaultLoadingController.loadSpeciesTreeData(filepath, errorListener);
        // setLoadedState();
        //    list.actionPerformed(new ActionEvent(this, 1, "Species loaded"));
        }
}//GEN-LAST:event_loadTreeButtonActionPerformed
    public void setLoadedState() {
        loadTreeButton.setEnabled(false);
        chooseFile.setEnabled(false);
        if (PluginDataHandle.getLoadedDataHandle().isProteinsLoaded()) {
            cleanButton.setEnabled(false);
        } else {
            cleanButton.setEnabled(true);
        }

        setFilenameLabel();
    }

    public void setUnloadedState() {
        loadTreeButton.setEnabled(true);
        chooseFile.setEnabled(true);
        cleanButton.setEnabled(false);
        setFilenameLabel();
    }

    private void setFilenameLabel() {
        String filename = PluginDataHandle.getLoadingDataHandle().getExpFilename();
        if (filename != null) {
            filenameLabel.setText(filename);
            filepath = filename;
        } else {
            loadTreeButton.setEnabled(false);
        }
    }

    private void cleanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cleanButtonActionPerformed
        UIController.getInstance().deleteData();
        setUnloadedState();
}//GEN-LAST:event_cleanButtonActionPerformed

    private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
        //CSH.DisplayHelpFromSource csh = new CSH.DisplayHelpFromSource(PPINEHelpBroker.getHelpBroker("Species file format"));
        //csh.actionPerformed(new ActionEvent(this, 120, "Species file format"));
        HelpBroker helpBroker = PPINEHelpBroker.getHelpBroker("Experiments");
        //CSH.DisplayHelpFromSource csh = new CSH.DisplayHelpFromSource(helpBroker);
        helpBroker.initPresentation();
        helpBroker.setDisplayed(true);
}//GEN-LAST:event_helpButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseFile;
    private javax.swing.JButton cleanButton;
    private javax.swing.JLabel filenameLabel;
    private javax.swing.JButton helpButton;
    private javax.swing.JButton loadTreeButton;
    // End of variables declaration//GEN-END:variables
}
